package com.base.lib.base.adapter

import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.ConcatAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.ethanhua.skeleton.Skeleton
import com.ethanhua.skeleton.SkeletonScreen
import com.base.lib.R
import com.base.lib.base.view.BaseActivity

/**
 * 骨架屏
 * @time 2021/7/10
 */
abstract class BaseSkeletonAdapter<E, VH : BaseViewHolder>(
    @LayoutRes open val layoutResId: Int,
    data: MutableList<E>? = null,
) : BaseQuickAdapter<E, VH>(layoutResId, data), LoadMoreModule {

    private var activity: BaseActivity? = null

    open var skeletonScreen: SkeletonScreen? = null

    /**
     * 骨架屏
     * @param recyclerView view
     * @param layoutId 骨架屏ui
     * @param itemCount 显示item数量
     */
    open fun bindSkeletonScreen(
        recyclerView: RecyclerView,
        @LayoutRes layoutId: Int, itemCount: Int,
    ) {
        super.recyclerView = recyclerView
        skeletonScreen = Skeleton.bind(recyclerView)
            .adapter(this) //设置实际adapter
            .shimmer(true) //是否开启动画
            .angle(30) //shimmer的倾斜角度 闪过去得阴影
            .color(R.color.color_88FFFFFF) //shimmer的颜色
            .frozen(true) //true则表示显示骨架屏时，RecyclerView不可滑动，否则可以滑动
            .duration(800) //动画时间，以毫秒为单位
            .count(itemCount) //显示骨架屏时item的个数 默认十个
            .load(layoutId)     // 骨架屏UI
            .show()
    }

    override fun setNewInstance(list: MutableList<E>?) {
        super.setNewInstance(list)
        hideSkeleton()
    }

    override fun setNewData(data: MutableList<E>?) {
        super.setNewInstance(data)
        hideSkeleton()
    }

    override fun replaceData(newData: Collection<E>) {
        super.setList(newData)
        hideSkeleton()
    }

    open fun hideSkeleton() {
        skeletonScreen?.hide()
    }

    open fun setNewDataByDiff(newNotes: List<E>) {
        val noteDiffUtil = NoteDiffUtil(data, newNotes)
        val diffResult = DiffUtil.calculateDiff(noteDiffUtil)
        data.clear()
        data.addAll(newNotes)
        diffResult.dispatchUpdatesTo(this)
    }

    /**
     * Create a [ConcatAdapter] with the provided [LoadStateAdapter]s displaying the
     */
    fun withLoadStateFooter(footer: CustomerFooterAdapter<*>): ConcatAdapter {
//        addLoadStateListener { loadStates ->
//            footer.loadState = loadStates.append
//        }
        return ConcatAdapter(this, footer)
    }

    internal class NoteDiffUtil<E>(oldNoteList: List<E>, newNoteList: List<E>) :
        DiffUtil.Callback() {
        var oldNoteList: List<E> = oldNoteList
        var newNoteList: List<E> = newNoteList

        override fun getOldListSize(): Int {
            return oldNoteList.size
        }

        override fun getNewListSize(): Int {
            return newNoteList.size
        }

        override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldNoteList[oldItemPosition].toString() === newNoteList[newItemPosition].toString()
        }

        override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
            return oldNoteList[oldItemPosition].toString() == (newNoteList[newItemPosition]).toString()
        }

        override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
            return super.getChangePayload(oldItemPosition, newItemPosition)
        }

    }
}